<?php
require_once "jssdk.php";


$数据=pdo()->selectColumns('public_serv_accounts',array('AppSecret','app_id'),array('id'=>$_SESSION['vo用户数据']['公众号ID']));

$jssdk = new JSSDK($数据['app_id'], $数据['AppSecret']);
$signPackage = $jssdk->getSignPackage();
?>
<!DOCTYPE html>
<html>
<head>
	<?php
	/**
	 * Created by PhpStorm. User: ADun, Date: 14-10-15 下午10:11
	 * Description:
	 */
	$openId=getCurrentUserOID();
	if(empty($openId)) exit('参数错误');
	$bm = new CUI数据中心(Page::b病历管理,$openId);
	$语音课程详情 = $bm->生成有效链接('Seller17Shipin_Lessons',$_GET['state']);
	$语音课程 = $bm->fc查语音课程($_GET['id']);
	?>
	<meta charset='UTF-8' />
	<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no"/>
	<link rel="stylesheet" href="style/common2.css">
	<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"> </script>

	<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
	<script src="../resource/js/jquery-1.11.1.min.js"></script>
	<script type="text/javascript" src="../js/iscroll.js"></script>
	<!-- Include all compiled plugins (below), or include individual files as needed -->
	<script src="../resource/js/bootstrap.min.js"></script>
	<script src="../resource/js/hermes.js"></script>

</head>
<body onUnload="myClose()">
<?php
$用户信息 =pdo()->selectOne("SELECT `头像` FROM `用户` WHERE `id` ='".$_SESSION['vo用户数据']['id']."';");
$课程id=$_GET['id'];
$time=date("Y-m-d H:i:s",time());
$是否报名=pdo()->selectOne("SELECT * FROM `语音课程报名` a WHERE a.`用户id`='".$_SESSION['vo用户数据']['id']."' AND a.`课程id`='".$课程id."' and a.`状态`='报名';");

if(!$是否报名){
    echo "<script>alert('您没有报名');location.replace(location.href='".$语音课程详情."&id=".$课程id."');</script>";
}
?>



<script language="javascript" type="text/javascript">
	var localId = '';
	var serverId = '';

	wx.config({
		debug: false,
		appId: '<?php echo $signPackage["appId"];?>',
		timestamp: <?php echo $signPackage["timestamp"];?>,
		nonceStr: '<?php echo $signPackage["nonceStr"];?>',
		signature: '<?php echo $signPackage["signature"];?>',
		jsApiList: [
			'startRecord',
			'stopRecord',
			'onRecordEnd',
			'playVoice',
			'pauseVoice',
			'stopVoice',
			'uploadVoice',
			'downloadVoice',
		]
	});
	$(document).ready(function(){
		//create a new WebSocket object.
//		var wsUri = "ws://localhost:9090/linhe_hospital/server.php";
		var wsUri = "ws://<?= G_websocket接口.":".G_websocket端口."/".G_websocket地址?>/server.php";

		websocket = new WebSocket(wsUri);
		websocket.onopen = function(ev) { // connection is open
			$('#message_box').append("<div class=\"system_msg\">Connected!</div>"); //notify user
		}

		$('.send-btn').click(function(){ //use clicks message send button
			var mymessage = $('#message').val(); //get message text
			var userid = $('#userid').val(); //get user id
			var headpic = $('#headpic').val(); //get user 头像
			var 课程id = $('#课程id').val(); //get 课程id
			var 群id = ""; //get 课程id
			var serverId="";
			
			if(mymessage == "" || mymessage == null || mymessage == " "){ //emtpy message?
				alert("请输入文字!");
				return;
			}

			//prepare json data
			var msg = {
				message: mymessage,
				userid: userid,
				headpic: headpic,
				课程id: 课程id,
				群id: 群id,
				serverId:serverId,
			};
			$.post('agent_manages.php',{'message':mymessage,"userid":userid,"serverId":serverId,'headpic':headpic,"课程id":课程id,'参数':'聊天记录'});
			$("#message").val("");
			//convert and send data to server
			websocket.send(JSON.stringify(msg));
		});

		//#### Message received from server?
		websocket.onmessage = function(ev) {
			var msg = JSON.parse(ev.data); //PHP sends Json data
			var type = msg.type; //message type
			var uid = msg.userid; //user name
			var upic = msg.headpic; //user name
			var u课程id = msg.课程id; //user name
			var u群id = msg.群id; //user name
			var userver = msg.serverId; //message text
			var 课程id = $('#课程id').val();
			var userid = $('#userid').val(); //get user id
			var umsg = msg.message; //message text
			if(u课程id!=""){
				if(u课程id==课程id){
					if(type == 'usermsg')
					{
						if(userver!=""){
							if(uid==userid){
								$('#message_box').append('<div class="chat-item-m"> <div class="style2"><div class="chat-item-img"><img  src="'+upic+'"></div></div><div class="chat-item-contr"><span class="font" onclick="bofang(\''+userver+'\')" ><span id="'+userver+'"><img  src="../style/xiaohongdian.gif" alt=""></span ><img style="padding-top:3px;" src="../style/yuyincont2.gif" alt=""></span></div><div class="clear"></div></div>')
							}else{
								$('#message_box').append('<div class="chat-item-o"><div class="style1"><div class="chat-item-img"><img src="'+upic+'"></div></div><div class="chat-item-contl"><span class="font" onclick="bofang(\''+userver+'\')"><img src="../style/yuyincont.gif" style="padding-top:3px;" alt=""><span id="'+userver+'"><img  src="../style/xiaohongdian.gif" alt=""></span></span></div><div class="clear"></div></div>')
							}
						}else{
							if(uid==userid){
								$('#message_box').append('<div class="chat-item-m"><div class="style2"><div class="chat-item-img"><img src="'+upic+'" ></div></div><div class="chat-item-contr"><span class="font">'+ umsg +'</span></div><div class="clear"></div></div>')
							}else{
								$('#message_box').append('<div class="chat-item-o"><div class="style1"><div class="chat-item-img"><img src="'+upic+'" ></div></div><div class="chat-item-contl"><span class="font">'+ umsg +'</span></div><div class="clear"></div></div>')
							}
						}

					}
				}

			}
		};

		websocket.onerror	= function(ev){$('#message_box').append("<div class=\"system_error\">Error Occurred - "+ev.data+"</div>");};
	});
	function myClose(){
		websocket.onclose();
	}
</script>
<div id="chat">
	<header>
		<div id="show123" class="news-btn" style="width:20%;">历史</div>
		<div class="chat_name" style="position:fixed;left:20%;"><?= $语音课程['name']?></div>
		<div  style="position:fixed;line-height: 50px;text-align: center;width:20%;opacity:0;min-width: 20%;"><input type="button"  id="查询起始值" name="查询起始值" onclick="查看历史()"  value="1" /></div>
	</header>
	<div id="wrapper">
		<div id="scroller">
			<div id="pullDown">
				<span class="pullDownIcon"></span><span class="pullDownLabel">Pull down to refresh...</span>
			</div>
			<div class="content" id="message_box" style="padding-bottom:90px;padding-top:50px;">
				<!-- <div id="content">
				</div> -->
			</div>
		</div>
	</div>
	<footer>
		<!--		<div class="input-btn1" style="display:block"> <img src="style/shuohua.gif" alt=""  ></div>-->
		<div class="wenzi" >
			<div class="input-btn11" style="display:block" onclick="fun11111(1)"> <img src="../style/shuohua.gif" alt=""  ></div>
			<div class="send-btn">发 送</div>
			<div class="input-text">
				<input type="hidden" name="userid" id="userid" value="<?=$_SESSION['vo用户数据']['id'];?>" />
				<input type="hidden" name="headpic"id="headpic" value="<?=$用户信息['头像']?>"/>
				<input type="hidden" name="课程id"  id="课程id" value="<?=$课程id?>"/>
				<input type="text" name="message" id="message" />
			</div>

		</div>
		<div class="yuyin">
			<div class="input-btn12" style="display:block"  onclick="fun11111(2)"> <img src="../style/shuohua.gif" alt=""  ></div>
			<div class="input-text1" onclick="xianshi(1,60)"><strong>开 始 说 话</strong></div>
			<div class="input-text2" style="display: none" onclick="xianshi(2,60)"><strong >结 束 说 话</strong></div>
		</div>
	</footer>
	<div class="time-bg"><div class="time">60s</div><div class="time-img"><img src="../style/huatong.gif" alt=""></div></div>
	<div class="floorbg"></div>
</div>
</body>
</html>
<script language="JavaScript">
	function 查看历史(){
		var page = $('#查询起始值').val();
		$.post('agent_manages.php',{'page':page,"课程id":'<?=$_GET['id']?>',"时间":'<?=$time?>','参数':'查看课程历史消息'},function(data){
			json = eval(data);

			str ='';

			for(var i=0; i<json.length; i++)
			{
				if(json[i].result2){
					str+='<p>'+json[i].result2+'</p>';
					$('#show123').remove();
					$('#查询起始值').remove();
				}


				var 图像="../cache/card_img/top_index.jpg";
				if(json[i].头像 !='') 图像 = json[i].头像;
				var 本用户 ='<?=$_SESSION['vo用户数据']['id']?>';

				if(json[i].serverid != null &&json[i].serverid != ''){

					if( 本用户 == json[i].用户id){
						str+=' <div class="chat-item-m"> <div class="style2"><div class="chat-item-img "><img src="'+图像+'"  ></div></div><div> <div class="chat-item-contr"> <span class="font" > <span><img onclick="playaudio(\''+json[i].serverid+'\')"  src="style/yuyincont2.gif" alt=""> <audio  id="'+json[i].serverid+'" src="http://www.<?php echo $_SERVER['HTTP_HOST']?>/Ram_disk/'+json[i].serverid+'.mp3" ></audio></span></span></div></div><div class="clear"></div></div> ';
					}else{
						str+='  <div class="chat-item-o"><div class="style1"><div class="chat-item-img"><img src="'+图像+'"  ></div></div><div class="chat-item-contl"><span class="font"> <img onclick="playaudio(\''+json[i].serverid+'\')" src="style/yuyincont.gif"   alt=""><span><audio  id="'+json[i].serverid+'" src="http://www.<?php echo $_SERVER['HTTP_HOST']?>/Ram_disk/'+json[i].serverid+'.mp3" ></audio></span> </span></div><div class="clear"></div></div> ';
					}


				}else{
					if( 本用户 == json[i].用户id){
						str+=' <div class="chat-item-m"><div class="style2"> <div class="chat-item-img"> <img src="'+图像+'" alt=""> </div> </div> <div class="chat-item-contr"> <span class="font">'+json[i].内容+'</span> </div> <div class="clear"> </div> </div> ';
					}else{
						str+='  <div class="chat-item-o"><div class="style1"><div class="chat-item-img"><img src="'+图像+'" alt=""></div></div> <div class="chat-item-contl"><span class="font">'+json[i].内容+'</span> <div class="clear"></div> </div></div> ';
					}
				}



			}

			$(str).prependTo("#message_box");

			$('#查询起始值').val(page*1 + 1);
			// alert(data);

		});

	}



	function fun11111(num3){
		if(num3==1){
			$("footer .input-btn12").children().attr("src","style/jianpan.gif");
			$(".wenzi").hide().next().show();
			$(".floorbg").show();
		}else
		{
			$(".yuyin").hide().prev().show();
			$("footer .input-btn11").children().attr("src","style/shuohua.gif");

		}
	}


	function xianshi(num2,num){

		if(num2==1){
			$(".time-bg").show();
			$(".input-text2").show();
			$(".input-text1").hide();
			setTimeout(show123(num),1000);
			$(".floorbg").show();
			startluyin();
		}else
		{

			$(".time-bg").hide();
			$(".input-text1").show();
			$(".input-text2").hide();
			clearTimeout(dingshi);
			$(".floorbg").hide();
			stopluyin();
		}
	}



	function show123(变量) {
		$(".time").text(变量+"s");
		var now=变量-1;
		if (parseInt(变量) > 0) {
			dingshi=setTimeout(function () {
				show123(now);
			}, 1000);
		}
	}


	function startluyin(){
		wx.startRecord({
			cancel: function () {
				alert('用户拒绝授权录音');
			}
		});
	}

	function stopluyin(){
		wx.stopRecord({
			success: function (res) {
				localId = res.localId;
				wx.uploadVoice({
					localId: localId,
					success: function (res) {
						//alert('上传语音成功，serverId 为' + res.serverId);
						serverId = res.serverId;
						var userid = $('#userid').val(); //get user id
						var headpic = $('#headpic').val(); //get user 头像
						var 课程id = $('#课程id').val(); //get 课程id
						var 群id = ""; //get 课程id
						var message = ""; //get 课程id
						$.post('agent_manages.php',{'serverId':serverId,'message':message,"userid":userid,'headpic':headpic,"课程id":课程id,'参数':'聊天记录'},function(data){

							var msg = {
								serverId: serverId,
								userid: userid,
								message: message,
								headpic: headpic,
								课程id: 课程id,
								群id: 群id,
							};
							websocket.send(JSON.stringify(msg));
						});

					}
				});
			},
			fail: function (res) {
				alert(JSON.stringify(res));
			}
		});
	}
	function bofang(serverId){
		//	alert(serverId);
		if (serverId == '') {
			alert('请先使用 uploadVoice 上传声音');
			return;
		}
		hidens(serverId);
		wx.downloadVoice({
			serverId: serverId,
			success: function (res) {
				//alert('下载语音成功，localId 为' + res.localId);
				localId = res.localId;
				wx.playVoice({
					localId: localId
				});
			}
		});
	}

	function hidens(num){
		$('#'+num).hide();
	}
	$(function(){
		$('#message').focus();
		$('#message').val(" ");
	});

	function playaudio($voiceid){
		$("#"+$voiceid)[0].play();
	}
</script>